import numpy as np
from matplotlib import pyplot as plt
from scipy import signal
from sklearn import preprocessing
from 遍历国内数据读取文件夹 import read_inM
for path in read_inM(r"D:\Codeware\Pythoncode\日本kik台网数据处理\日本kik数据处理\IWT0129805110832"):
    print(path)
    total_data = [];
    i=0;
    for line in open(path):
        i=i+1
        # print(float((line[:-1].split(",")[4])))
        total_data.append(float((line[:-1].split(",")[2])));

    # 创建一个MinMaxScaler对象
    # scaler = preprocessing.MinMaxScaler(feature_range=(0, 1))
    # # 使用fit_transform方法对数据进行归一化处理
    # normalized_data = scaler.fit_transform([[x] for x in total_data])
    # total_data =normalized_data;
    # 进行基线校正
    # total_data = signal.detrend(total_data)

    i = 0  # 重置0
    #     for li in range(len(liness)-1):
    #         i = i + 1 #记录条数
    #         sumL = 0;
    #         sumS = 0;  # 初始化为0
    #         sumML = 0;
    #         sumMS = 0;
    #         if i > nl+ns:
    #             # z=1;
    #             # 长窗
    #             for k in range(i - 1-ns, i - nl-ns, -1):
    #                 # sumL = sumL + liness[k]*liness[k]-(liness[k-1]*liness[k+1])
    #                 # sumML = sumML + liness[k]
    #                 # sumL = sumL + (liness[k]-sumML/z)* (liness[k]-sumML/z);
    #                 # z=z+1
    #                 # sumL = sumL + abs(liness[k])
    #                 # sumL = sumL + abs(liness[k]-liness[k-1])
    #                 sumL = sumL +(liness[k]*liness[k])+(liness[k]-liness[k-1])*(liness[k]-liness[k-1])
    #             lta = sumL/nl;
    #             # 短窗
    #             # z=0;
    #             for k in range(i - 1, i - nl, -1):
    #                 # z = z + 1;
    #                 # sumS = sumS + liness[k] * liness[k] - (liness[k - 1] * liness[k + 1])
    #                 # sumMS = sumMS + liness[k]
    #                 # sumS = sumS + (liness[k] - sumMS / z) * (liness[k] - sumMS / z);
    #                 # sumS = sumS + abs(liness[k])
    #                 # sumS = sumS + abs(liness[k]-liness[k-1])
    #                 sumS = sumS + (liness[k] * liness[k]) + (liness[k] - liness[k - 1]) * (liness[k] - liness[k - 1])
    #             sta = sumS/ns;
    #             p.append(sta/lta)
    #     #补齐时窗
    #     print(p)
    #     # for i in range(121):
    #     #     p.append(p[0])
    #
    #     # print(len(p))
    #     x = np.linspace(0, ((0 + len(p) - 1) * 0.005), len(p))
    #     plt.plot(p,label='STA/LTA')
    #     # plt.axhline(y=5.5,c='r',label='阈值(R)')
    #     plt.title("sta,lta完整图形")
    #     # plt.ylim(-1,6)
    #     # plt.ylim(-1, 10)
    #     plt.xlabel("t/s")
    #     plt.ylabel("STA/LTA")
    #     plt.legend()
    #     plt.show()


    # 原始数据图像
    x = np.linspace(0, ((0 + len(total_data) - 1)*0.02), len(total_data))
    ax = plt.gca()
    plt.text(0, 1, path, fontsize=10, color='green', transform=ax.transAxes)
    plt.plot(x,total_data)
    # plt.xlim(1, 100)
    file_name = path.split("\\")[-1].split('.')[0]
    print(file_name)
    plt.savefig(r'微震图片/{}'.format(file_name))
    plt.show()
    plt.close()


